package repository

import (
	"gorm.io/gorm"
	"shop-bean/model"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
)

type ITransportRepository interface {
	FindById(db *gorm.DB, id uint) (transport *model.Transport, err error)
	Find(db *gorm.DB, shopId uint) (transports []*model.Transport, err error)
	CheckExist(db *gorm.DB, transName string) (bool, error)
	FindPage(db *gorm.DB, params *pagehelper.Params) ([]*model.Transport, error)
	Count(db *gorm.DB, params *pagehelper.Params) (int64, error)
	Create(db *gorm.DB, transport *model.Transport) error
	Updates(db *gorm.DB, transport *model.Transport) error
	Delete(db *gorm.DB, id uint) error
	DeleteAssociation(db *gorm.DB, transfees []*model.Transfee, transfeeFrees []*model.TransfeeFree) error
}

type TransportRepository struct {
}

func NewTransportRepository() ITransportRepository {
	return &TransportRepository{}
}

func (r *TransportRepository) FindById(db *gorm.DB, id uint) (transport *model.Transport, err error) {
	return transport, db.First(&transport, id).Error
}

func (r *TransportRepository) Find(db *gorm.DB, shopId uint) (transports []*model.Transport, err error) {
	return transports, db.Model(&model.Transport{}).Where("shop_id = ?", shopId).Find(&transports).Error
}
func (r *TransportRepository) FindPage(db *gorm.DB, params *pagehelper.Params) (page []*model.Transport, err error) {
	tx := queryWrapper.ReuseWherePair(db.Model(&model.Transport{}), params.WherePair)
	return page, tx.Scopes(pagehelper.Paginate(params.Current, params.Size)).Find(&page).Error
}

func (r *TransportRepository) CheckExist(db *gorm.DB, transName string) (exist bool, err error) {
	var count int64
	err = db.Select("transport_id").Model(&model.Transport{}).Where("trans_name = ?", transName).Count(&count).Error
	if err != nil {
		return false, err
	}
	return count > 0, nil
}

func (r *TransportRepository) Count(db *gorm.DB, params *pagehelper.Params) (count int64, err error) {
	tx := queryWrapper.ReuseSelect(db.Model(&model.Transport{}), params.SelectFields)
	return count, queryWrapper.ReuseWherePair(tx, params.WherePair).Count(&count).Error
}

func (r *TransportRepository) Create(db *gorm.DB, transport *model.Transport) error {
	return db.Create(&transport).Error
}

func (r *TransportRepository) Updates(db *gorm.DB, transport *model.Transport) error {
	return db.Updates(&transport).Error
}

func (r *TransportRepository) Delete(db *gorm.DB, id uint) error {
	return db.Delete(&model.Transport{}, id).Error
}

func (r *TransportRepository) DeleteAssociation(db *gorm.DB, transfees []*model.Transfee, transfeeFrees []*model.TransfeeFree) error {
	if len(transfees) > 0 {
		for _, transfee := range transfees {
			if err := db.Model(&transfee).Association("CityList").Delete(&transfee.CityList); err != nil {
				return err
			}
		}
	}

	if len(transfeeFrees) > 0 {
		for _, transfeeFree := range transfeeFrees {
			if err := db.Model(&transfeeFree).Association("FreeCityList").Delete(&transfeeFree.FreeCityList); err != nil {
				return err
			}
		}
	}

	return nil
}
